package com.guoyh.procommon

import android.os.Bundle
import androidx.fragment.app.Fragment
import com.guoyh.baseutils.viewModel.SupportViewModel
import com.guoyh.mcommon.R
import com.guoyh.mcommon.databinding.MBaseAloneActivityBinding
import com.guoyh.procommon.base.XBaseActivity
import com.guoyh.procommon.base.XDBaseActivity

abstract class BaseAloneActivity:
    XDBaseActivity<MBaseAloneActivityBinding>() {

    override val layoutId = R.layout.m_base_alone_activity

    private val BUNDLE_FRAGMENTS_KEY = "android:support:fragments"


    override fun onCreate(savedInstanceState: Bundle?) {
        InitAll.initAll(this.application)
        if (savedInstanceState != null && clearFragmentsTag()) {
            //重建时清除 fragment的状态
            savedInstanceState.remove(BUNDLE_FRAGMENTS_KEY)
        }
        super.onCreate(savedInstanceState)
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        if (outState != null && clearFragmentsTag()) {
            //销毁时不保存fragment的状态
            outState.remove(BUNDLE_FRAGMENTS_KEY)
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        //开启事务
        val beginTransaction = supportFragmentManager.beginTransaction()
        beginTransaction.replace(R.id.group, getFragment())
        //最后一步 记得commit
        beginTransaction.commit()
    }

    protected open fun clearFragmentsTag(): Boolean {
        return true
    }

    abstract fun getFragment(): Fragment
}